package com.ml.ws.config;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ml.ws.handler.TradingWebSockeHandler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * 初始化链接时候的组件
 */
@Component
public class TradingWebSocketChannelHandler extends ChannelInitializer<SocketChannel> {

	@Autowired
	TradingWebSockeHandler tradingWebSockeHandler;
	
    @Override
    protected void initChannel(SocketChannel e) throws Exception {

        e.pipeline().addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
        e.pipeline().addLast("http-codec", new HttpServerCodec());
        e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        e.pipeline().addLast("handler", tradingWebSockeHandler);
        
    }
}
